<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>直播列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        h1 {
            text-align: center;
        }

        table {
            width: 80%;
            margin: 0 auto;
            border-collapse: collapse;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        #addUserForm {
            width: 50%;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
            background-color: #f9f9f9;
        }

        #addUserForm label {
            display: block;
            margin-bottom: 10px;
        }

        #addUserForm input[type="text"],
        #addUserForm input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        #addUserForm input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>

<body>
<h1>用户管理</h1>

<table id="userTable">
    <tr>
        <th>ID</th>
        <th>用户名</th>
        <th>密码</th>
        <th>操作</th>
    </tr>
</table>

<h2>添加用户</h2>
<form id="addUserForm">
    <label for="username">用户名：</label>
    <input type="text" id="username" name="username">
    <label for="password">密码：</label>
    <input type="password" id="password" name="password">
    <input type="submit" value="添加">
</form>

<script>
    // 初始化页面时获取用户数据并显示
    function init() {
        fetchUsers();
    }

    // 获取用户数据
    function fetchUsers() {
        fetch('/user')
            .then(response => response.json())
            .then(data => {
                const table = document.getElementById('userTable');
                table.innerHTML = '';
                data.forEach(user => {
                    const row = table.insertRow();
                    const idCell = row.insertCell(0);
                    const usernameCell = row.insertCell(1);
                    const passwordCell = row.insertCell(2);
                    const actionCell = row.insertCell(3);

                    idCell.innerHTML = user.id;
                    usernameCell.innerHTML = user.username;
                    passwordCell.innerHTML = user.password;

                    const editButton = document.createElement('button');
                    editButton.innerHTML = '编辑';
                    editButton.onclick = () => editUser(user.id);

                    const deleteButton = document.createElement('button');
                    deleteButton.innerHTML = '删除';
                    deleteButton.onclick = () => deleteUser(user.id);

                    actionCell.appendChild(editButton);
                    actionCell.appendChild(deleteButton);
                });
            })
            .catch(error => console.error('获取用户数据时出错:', error));
    }

    // 添加用户
    document.getElementById('addUserForm').addEventListener('submit', function (event) {
        event.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        fetch('/user', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ username, password })
        })
            .then(response => {
                if (response.status === 200) {
                    fetchUsers();
                    document.getElementById('username').value = '';
                    document.getElementById('password').value = '';
                } else {
                    console.error('添加用户失败');
                }
            })
            .catch(error => console.error('添加用户时出错:', error));
    });

    // 编辑用户
    function editUser(id) {
        const user = {
            id: id,
            username: prompt('请输入新的用户名：'),
            password: prompt('请输入新的密码：')
        };

        fetch(`/user/${id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(user)
        })
            .then(response => {
                if (response.status === 200) {
                    fetchUsers();
                } else {
                    console.error('编辑用户失败');
                }
            })
            .catch(error => console.error('编辑用户时出错:', error));
    }

    // 删除用户
    function deleteUser(id) {
        fetch(`/user/${id}`, {
            method: 'DELETE'
        })
            .then(response => {
                if (response.status === 200) {
                    fetchUsers();
                } else {
                    console.error('删除用户失败');
                }
            })
            .catch(error => console.error('删除用户时出错:', error));
    }

    init();
</script>
</body>

</html>